package com.cloudea.learn.layer;

import com.cloudea.learn.Tensor;


/**
 * elu激活函数
 * @author cloudea
 * @date 2022/01/03
 */
public class Elu extends Layer {

    private final double alpha;

    public Elu(){
        this(1);
    }

    public Elu(double alpha){
        this.alpha = alpha;
    }

    @Override
    protected Tensor forward(Context context, Tensor... inputs) {
        return Operator.elu(inputs[0], alpha);
    }

    @Override
    public Tensor[] backward(Context context, Tensor grid, Tensor... inputs) {
        Tensor x = inputs[0];
        Tensor gridX = new Tensor(x.getShape());
        for(int i = 0; i < x.getSize(); i++){
            double xi = x.getByLocation(i);
            gridX.setByLocation(i, grid.getByLocation(i) *  (xi > 0.0 ? 1.0 : alpha * Math.exp(xi)));
        }
        return new Tensor[]{gridX};
    }
}